package com.example.demo.WebSocket;

import com.example.demo.Match.Match;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.example.demo.Info.*;
@Component
public class Game_Hall_Socket extends TextWebSocketHandler {
    @Autowired
    private Match match;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private Websocket_And_Room_Manage websocket_and_room_manage;
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        int userid=((user_info)session.getAttributes().get("user")).getUserid();
        if (websocket_and_room_manage.game_hall_websocket.get(userid)==null){
            websocket_and_room_manage.game_hall_websocket.put(userid,session);
        }else{
            Game_Hall_Websocket_Response gameHallWebsocketResponse=new Game_Hall_Websocket_Response();
            gameHallWebsocketResponse.setMessage("repeatConnection");
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(gameHallWebsocketResponse)));
        }
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        user_info userInfo=(user_info)session.getAttributes().get("user");
        String payload= message.getPayload();
        Game_Hall_Websocket_Request gameHallWebsocketRequest=objectMapper.readValue(payload,Game_Hall_Websocket_Request.class);
        if (gameHallWebsocketRequest.getMessage().equals("startMatch")){
            if (match.matching(userInfo)){
                Game_Hall_Websocket_Response gameHallWebsocketResponse=new Game_Hall_Websocket_Response();
                gameHallWebsocketResponse.setMessage("startMatch");
                session.sendMessage(new TextMessage(objectMapper.writeValueAsString(gameHallWebsocketResponse)));
            }
        }else if (gameHallWebsocketRequest.getMessage().equals("stopMatch")){
            match.unmatching(userInfo);
            Game_Hall_Websocket_Response gameHallWebsocketResponse=new Game_Hall_Websocket_Response();
            gameHallWebsocketResponse.setMessage("stopMatch");
            session.sendMessage(new TextMessage(objectMapper.writeValueAsString(gameHallWebsocketResponse)));
        }
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        user_info userInfo=(user_info)session.getAttributes().get("user");
        if (websocket_and_room_manage.game_hall_websocket.get(userInfo.getUserid())!=null) {
            websocket_and_room_manage.game_hall_websocket.remove(userInfo.getUserid());
            match.unmatching(userInfo);
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        user_info userInfo=(user_info)session.getAttributes().get("user");
        if (websocket_and_room_manage.game_hall_websocket.get(userInfo.getUserid())!=null) {
            websocket_and_room_manage.game_hall_websocket.remove(userInfo.getUserid());
            match.unmatching(userInfo);
        }
    }
}
